package cn.wolfcode.mongo;

import cn.wolfcode.mongo.domain.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@SpringBootTest
public class MongoTest2 {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Test
    public void testSave(){
        User user = new User();
        user.setName("小x");
        user.setAge(20);
        mongoTemplate.save(user);
    }

    @Test
    public void testDelete(){
        Query query = new Query();
        Criteria criteria = Criteria.where("_id").is("5f0488dc1108c424be7820e5");
        query.addCriteria(criteria);
        mongoTemplate.remove(query,"user");
        mongoTemplate.remove(query,User.class);

    }

    @Test
    public void testUpdate(){
        User user = mongoTemplate.findById("5f04873e7fd3b204c59172d2", User.class);
        user.setName("111");
        mongoTemplate.save(user);
    }

    @Test
    public void testGet(){
        User user = mongoTemplate.findById("5f030b413d5a000067001812", User.class);
        System.out.println(user);
    }

    @Test
    public void testAll(){
        List<User> list = mongoTemplate.findAll(User.class);
        list.forEach(System.out::println);
    }
}
